﻿using HardcoreGame.Content.Buffs;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using Terraria;
using Terraria.GameContent;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.WorldBuilding;

namespace HardcoreGame.Content.Projectiles.Whips
{
    public class AirAlert : ModProjectile
    {
        private static Conditions.IsSolid _cachedConditions_solid = new();

        private static Conditions.NotNull _cachedConditions_notNull = new();
        public override void SetStaticDefaults()
        {
            ProjectileID.Sets.IsAWhip[Type] = true;
        }
        public override void SetDefaults()
        {
            Projectile.DefaultToWhip();
            Projectile.width = 18;
            Projectile.height = 18;
            Projectile.friendly = true;
            Projectile.penetrate = -1;
            Projectile.tileCollide = false;
            Projectile.extraUpdates = 1;
            Projectile.usesLocalNPCImmunity = true;
            Projectile.localNPCHitCooldown = -1;
            Projectile.DamageType = DamageClass.SummonMeleeSpeed;
            Projectile.ownerHitCheck = true;
            Projectile.WhipSettings.Segments = 26;
            Projectile.WhipSettings.RangeMultiplier = 1.625f;
        }
        public override void AI()
        {
            int timeToFlyOut = Main.player[Projectile.owner].itemAnimationMax * Projectile.MaxUpdates;
            bool bubbleTime = Projectile.ai[0] >= (int)(timeToFlyOut * 0.475f) && Projectile.ai[0] <= (int)(timeToFlyOut * 0.825f);
            List<Vector2> whipPointsForDust = [];
            Projectile.FillWhipControlPoints(Projectile, whipPointsForDust);
            float swingTime = Main.player[Projectile.owner].itemAnimationMax * Projectile.MaxUpdates;
            float swingProgress = Projectile.ai[0] / swingTime;
            if (Utils.GetLerpValue(0.1f, 0.7f, swingProgress, true) * Utils.GetLerpValue(0.9f, 0.7f, swingProgress, true) > 0.5f && bubbleTime)
            {
                int dust = Dust.NewDust(whipPointsForDust[^1], Projectile.height, Projectile.width, DustID.Ichor, 0f, 0f, 0, default, 2f);
                Main.dust[dust].velocity *= Main.rand.NextFloat(-1f, 1f) * 0.75f;
                Main.dust[dust].noGravity = true;
                Main.dust[dust].fadeIn = Main.rand.NextFloat() * 1.2f;
            }
        }
        public override void OnHitNPC(NPC target, NPC.HitInfo hit, int damageDone)
        {
            target.AddBuff(ModContent.BuffType<BeckysCurse>(), 600);
            Main.player[Projectile.owner].MinionAttackTargetNPC = target.whoAmI;
            Projectile.damage = (int)(Projectile.damage * 0.75f);
            if (!WorldUtils.Find(target.Center.ToTileCoordinates(), Searches.Chain(new Searches.Down(12), _cachedConditions_notNull, _cachedConditions_solid), out var _))
            {
                Projectile.damage = (int)(Projectile.damage * 1.2f);
            }
        }
        private static void DrawLine(List<Vector2> list)
        {
            Texture2D texture = TextureAssets.FishingLine.Value;
            Rectangle frame = texture.Frame();
            Vector2 origin = new(frame.Width / 2, 2);
            Vector2 pos = list[0];
            for (int i = 0; i < list.Count - 1; i++)
            {
                Vector2 element = list[i];
                Vector2 diff = list[i + 1] - element;
                float rotation = diff.ToRotation() - MathHelper.PiOver2;
                Color color = Lighting.GetColor(element.ToTileCoordinates(), Color.Yellow);
                Vector2 scale = new(1, (diff.Length() + 2) / frame.Height);
                Main.EntitySpriteDraw(texture, pos - Main.screenPosition, frame, color, rotation, origin, scale, SpriteEffects.None, 0);
                pos += diff;
            }
        }
        public override bool PreDraw(ref Color lightColor)
        {
            List<Vector2> list = [];
            Projectile.FillWhipControlPoints(Projectile, list);
            DrawLine(list);
            SpriteEffects spriteEffects = SpriteEffects.None;
            if (Projectile.spriteDirection == 1)
            {
                spriteEffects ^= SpriteEffects.FlipHorizontally;
            }
            Texture2D value = TextureAssets.Projectile[Type].Value;
            Rectangle rectangle = value.Frame(1, 5);
            int height = rectangle.Height;
            rectangle.Height -= 2;
            Vector2 vector = rectangle.Size() / 2f;
            Vector2 vector2 = list[0];
            for (int i = 0; i < list.Count - 1; i++)
            {
                Vector2 origin = vector;
                float scale = 1f;
                bool flag;
                if (i == 0)
                {
                    origin.Y -= 4f;
                    flag = true;
                }
                else
                {
                    flag = true;
                    int num = 1;
                    if (i > 10)
                    {
                        num = 2;
                    }
                    if (i > 20)
                    {
                        num = 3;
                    }
                    rectangle.Y = height * num;
                }
                if (i == list.Count - 2)
                {
                    flag = true;
                    rectangle.Y = height * 4;
                    Projectile.GetWhipSettings(Projectile, out var timeToFlyOut, out _, out _);
                    float t = Projectile.ai[0] / timeToFlyOut;
                    float amount = Utils.GetLerpValue(0.1f, 0.7f, t, clamped: true) * Utils.GetLerpValue(0.9f, 0.7f, t, clamped: true);
                    scale = MathHelper.Lerp(0.5f, 1.5f, amount);
                }
                Vector2 vector3 = list[i];
                Vector2 vector4 = list[i + 1] - vector3;
                if (flag)
                {
                    float rotation = vector4.ToRotation() - (float)Math.PI / 2f;
                    Color color = Lighting.GetColor(vector3.ToTileCoordinates());
                    Main.spriteBatch.Draw(value, vector2 - Main.screenPosition, rectangle, color, rotation, origin, scale, spriteEffects, 0f);
                }
                vector2 += vector4;
            }
            return false;
        }
    }
}
